package com.yatop.lalmbda.ai;

import com.yatop.lalmbda.ai.aliyun.mock.BgGenerationReq;
import com.yatop.lalmbda.ai.aliyun.mock.BgGenerationRes;
import com.yuyaogc.lowcode.engine.util.JsonUtil;
import com.yuyaogc.lowcode.engine.util.OkHttpClient;
import okhttp3.Response;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class AliyunMockApi {
    public final static String API_KEY = "sk-38016e7ba306415fb0e87bbab52f31a9";

    public static void main(String[] args) throws IOException {
        BgGenerationRes res =  background_generation();
        Util.waitAsyncTask(res.getOutput().getTask_id());
    }


    public static BgGenerationRes background_generation() throws IOException {
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/background-generation/generation/";

        Map<String,String> headers = new HashMap<>();
        headers.put("X-DashScope-Async","enable");
        headers.put("Authorization","Bearer " + API_KEY);
        headers.put("Content-Type","application/json");


        String j = "{\n" +
                "    \"model\": \"wanx-background-generation-v2\",\n" +
                "    \"input\": {\n" +
                "        \"base_image_url\": \"http://123.207.67.48/223213213.png\",\n" +
                "        \"ref_image_url\": \"http://123.207.67.48/eeeeeeee.jpg\"\n" +
                "    },\n" +
                "    \"parameters\": {\n" +
                "        \"model_version\": \"v3\",\n" +
                "        \"n\": 1,\n" +
                "        \"noise_level\": 300\n" +
                "    }\n" +
                "}";
//        BgGenerationReq bgGeneration = JsonUtil.readValue(j.getBytes(StandardCharsets.UTF_8), BgGenerationReq.class);

//        String json = JsonUtil.objectToString(bgGeneration);

        try (Response response = OkHttpClient.post(url, headers, j)){
            System.out.println(response.code());
//            System.out.println(response.headers());
            if( response.isSuccessful()){
                String r = response.body().string();
                System.err.println(r);
                BgGenerationRes res =  JsonUtil.stringToObject(r, BgGenerationRes.class);
                return res;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
